package org.jeecg.modules.flowable.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.modules.flowable.common.dic.ResultMessage;
import org.jeecg.modules.flowable.common.util.json.ObjectConvert;
import org.jeecg.modules.flowable.controller.dto.FengFormRequest;
import org.jeecg.modules.flowable.dic.DictKey;
import org.jeecg.modules.flowable.entity.TActFengFormEntity;
import org.jeecg.modules.flowable.mapper.ExtFlowableMapper;
import org.jeecg.modules.flowable.service.FormService;
import org.jeecg.modules.flowable.service.bo.FengForm;
import org.jeecg.modules.flowable.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 6/15/22 12:46 PM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
@Api(value = "FormManagerController", tags = {"(module-flowable)-表单管理"})
@ApiSort(value = 1)
@RestController
@RequestMapping("/FormManagerController")
@Slf4j
public class FormManagerController {

    @Autowired
    private FormService formService;

    @Autowired
    private ExtFlowableMapper extFlowableMapper;

    @RequiresRoles({"admin"})
    @ApiOperation(value = "查询表单列表", notes = "查询表单列表")
    @RequestMapping(path = "/queryFormList", method = RequestMethod.POST)
    @ResponseBody
    public Result<Page<FengForm>> queryFormList(String argFormName, String argFormKey, String argFormType, Integer argCurrPage, Integer argPageSize) {
        Page<FengForm> fengFormPage = formService.queryFormList(argFormName, argFormKey, argFormType, argCurrPage, argPageSize);
        return Result.OK(fengFormPage);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "查询有效的表单列表", notes = "查询表单列表")
    @RequestMapping(path = "/queryEffectiveFormList", method = RequestMethod.POST)
    @ResponseBody
    public Result<Page<FengForm>> queryEffectiveFormList(String argFormName, String argFormKey, String argFormType, Integer argCurrPage, Integer argPageSize) {
        Page<FengForm> fengFormPage = formService.queryEffectiveFormList(argFormName, argFormKey, argFormType, argCurrPage, argPageSize);
        return Result.OK(fengFormPage);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "跟据表单KEY查询表单列表", notes = "跟据表单KEY查询表单列表")
    @RequestMapping(path = "/queryFormListByKey", method = RequestMethod.POST)
    @ResponseBody
    public Result<Page<FengForm>> queryFormListByKey(String argKey, Integer argCurrPage, Integer argPageSize) {
        Page<FengForm> fengFormPage = formService.queryFormListByKey(argKey, argCurrPage, argPageSize);
        return Result.OK(fengFormPage);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "跟据ID查询表单信息", notes = "跟据ID查询表单信息")
    @RequestMapping(path = "/queryFormListById", method = RequestMethod.POST)
    @ResponseBody
    public Result<FengForm> queryFormListById(String argId) {
        FengForm fengForm = formService.queryFormListById(argId);
        return Result.OK(fengForm);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "保存", notes = "保存")
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result<FengForm> save(@RequestBody FengFormRequest argFengFormRequest) {
        TActFengFormEntity convert = ObjectConvert.convert(argFengFormRequest, TActFengFormEntity.class);
        FengForm fengForm = formService.save(convert);
        return Result.OK(fengForm);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "挂起", notes = "挂起")
    @RequestMapping(path = "/formSupend", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> formSupend(String argFormId) {
        formService.formSupend(argFormId);
        return Result.OK(ResultMessage.OK);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "激活", notes = "激活")
    @RequestMapping(path = "/formActive", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> formActive(String argFormId) {
        formService.formActive(argFormId);
        return Result.OK(ResultMessage.OK);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "查询表单类型", notes = "查询表单类型")
    @RequestMapping(path = "/queryFormType", method = RequestMethod.POST)
    @ResponseBody
    public Result<List<Map<String, Object>>> queryFormType() {
        List<Map<String, Object>> maps = extFlowableMapper.queryDictItem(DictKey.FLOWABLE_FORM_TYPE);
        return Result.OK(maps);
    }

    @RequiresRoles({"admin"})
    @ApiOperation(value = "跟据表单KEY查询表单", notes = "跟据表单KEY查询表单")
    @RequestMapping(path = "/queryFormByKey", method = RequestMethod.POST)
    @ResponseBody
    public Result<FengForm> queryFormByKey(String argKey) {
        FengForm fengForm = formService.queryFormByKey(argKey);
        return Result.OK(fengForm);
    }
}